﻿package _Pan3D.model{

	import flash.display.Sprite;
    import _Pan3D.cameras.Camera3D
	import _Pan3D.base.Object3D
    // --------------MSN:lation_pan@live.cn  QQ: 3423526------------- //

    public class M3D_Model extends Sprite {
        public static var _Cam:Camera3D;
        public var _this3d
		public var _addvertices=new Vector.<Number>();
		public var _adduvtData=new Vector.<Number>();
		public var _addind = new Vector.<int>();

		
		public function math_change_point(_3dpoint) {
			var rx = _3dpoint.x -_Cam.x 
			var ry = _3dpoint.y -_Cam.y
			var rz = _3dpoint.z -_Cam.z
	
			var tmp_rx = rx;
			rx = _Cam.cos_y * tmp_rx - _Cam.sin_y * rz;
			rz = _Cam.sin_y * tmp_rx + _Cam.cos_y * rz;
			var tmp_ry = ry;
			ry = _Cam.cos_x * tmp_ry - _Cam.sin_x * rz;
			rz = _Cam.sin_x * tmp_ry + _Cam.cos_x * rz;
				
			_3dpoint.rx = rx;
			_3dpoint.ry = ry;
			_3dpoint.rz = rz;
			
			_3dpoint.dx = rx / (rz + _Cam._br) * _Cam._br;
			_3dpoint.dy = -ry / (rz + _Cam._br) * _Cam._br;
			_3dpoint.dz = _Cam._br / (_Cam._br + rz);
		
		}
		
		public function get_suit_rec(_A0, _A1, _A2, _C0, _C1, _C2) {
			var _temp_max, _temp_mid, _temp_min;
			var _a0,_a1,_a2,_a3
			var _c0,_c1,_c2,_c3
			var _hitz = -_Cam._br + 10
			if (Math.max(_A0.rz, _A1.rz, _A2.rz) <= _hitz) {
				return;
			}
			_temp_mid = get_mid_z(_A0.rz, _A1.rz, _A2.rz)
			if (_temp_mid < _hitz) {
				if (_A0.rz > _hitz) {
					_a0={ rx:_A0.rx, ry:_A0.ry, rz:_A0.rz, dx:_A0.dx, dy:_A0.dy, dz:_A0.dz,ux:_C0.x,uy:_C0.y }
					_a1=_get_new_one_point(_A0, _A1, _C0, _C1,_hitz);
					_a2=_get_new_one_point(_A0, _A2, _C0, _C2,_hitz);
				}
				if (_A1.rz > _hitz) {
					_a0 = _get_new_one_point(_A1, _A0, _C1, _C0, _hitz);
					_a1 = { rx:_A1.rx, ry:_A1.ry, rz:_A1.rz, dx:_A1.dx, dy:_A1.dy, dz:_A1.dz, ux:_C1.x, uy:_C1.y }
					_a2 = _get_new_one_point(_A1, _A2, _C1, _C2, _hitz);
				}
				if (_A2.rz > _hitz) {
					_a0 = _get_new_one_point(_A2, _A0, _C2, _C0, _hitz);
					_a1 = _get_new_one_point(_A2, _A1, _C2, _C1, _hitz);
					_a2 = { rx:_A2.rx, ry:_A2.ry, rz:_A2.rz, dx:_A2.dx, dy:_A2.dy, dz:_A2.dz, ux:_C2.x, uy:_C2.y }
 
				}
				_addvertices.push(_a0.dx, _a0.dy, _a1.dx, _a1.dy, _a2.dx, _a2.dy)
				_adduvtData.push(_a0.ux,_a0.uy,_a0.dz,_a1.ux,_a1.uy,_a1.dz,_a2.ux,_a2.uy,_a2.dz)
				
			}else {
				if (_A0.rz < _hitz) {
			        _a0 = _get_new_one_point(_A0, _A1, _C0, _C1, _hitz);
					_a1= { rx:_A1.rx, ry:_A1.ry, rz:_A1.rz, dx:_A1.dx, dy:_A1.dy, dz:_A1.dz, ux:_C1.x, uy:_C1.y }
					_a2 = { rx:_A2.rx, ry:_A2.ry, rz:_A2.rz, dx:_A2.dx, dy:_A2.dy, dz:_A2.dz, ux:_C2.x, uy:_C2.y }
					_a3 = _get_new_one_point(_A0, _A2, _C0, _C2, _hitz);
				}
				if (_A1.rz < _hitz) {
					_a0 = { rx:_A0.rx, ry:_A0.ry, rz:_A0.rz, dx:_A0.dx, dy:_A0.dy, dz:_A0.dz, ux:_C0.x, uy:_C0.y }
					_a1 = _get_new_one_point(_A1, _A0, _C1, _C0, _hitz);
					_a2 = _get_new_one_point(_A1, _A2, _C1, _C2, _hitz);
					_a3 = { rx:_A2.rx, ry:_A2.ry, rz:_A2.rz, dx:_A2.dx, dy:_A2.dy, dz:_A2.dz, ux:_C2.x, uy:_C2.y }
				}
				if (_A2.rz < _hitz) {
			
					_a0 = { rx:_A0.rx, ry:_A0.ry, rz:_A0.rz, dx:_A0.dx, dy:_A0.dy, dz:_A0.dz, ux:_C0.x, uy:_C0.y }
					_a1 = { rx:_A1.rx, ry:_A1.ry, rz:_A1.rz, dx:_A1.dx, dy:_A1.dy, dz:_A1.dz, ux:_C1.x, uy:_C1.y }
					_a2 = _get_new_one_point(_A2, _A1, _C2, _C1, _hitz);
					_a3 = _get_new_one_point(_A2, _A0, _C2, _C0, _hitz);
					
				}
				
				_addvertices.push(_a0.dx, _a0.dy, _a1.dx, _a1.dy, _a2.dx, _a2.dy)
				_addvertices.push(_a0.dx, _a0.dy, _a2.dx, _a2.dy, _a3.dx, _a3.dy)
				_adduvtData.push(_a0.ux,_a0.uy,_a0.dz,_a1.ux,_a1.uy,_a1.dz,_a2.ux,_a2.uy,_a2.dz)
				_adduvtData.push(_a0.ux,_a0.uy,_a0.dz,_a2.ux,_a2.uy,_a2.dz,_a3.ux,_a3.uy,_a3.dz)
			}
			
		}

		private function _get_new_one_point(_in, _out, _C0, _C1,_hitz) {
			var rx, ry, rz, dx, dy, dz,ux,uy;
			var _n = Math.abs((_hitz - _in.rz) / (_out.rz - _in.rz));
			rx = _n * (_out.rx - _in.rx) + _in.rx;
			ry = _n * (_out.ry - _in.ry) + _in.ry;
			rz = _hitz;
	       
			ux = _n * (_C1.x - _C0.x) + _C0.x;
            uy = _n * (_C1.y - _C0.y) + _C0.y;
			
			dx = rx / (rz + _Cam._br) * _Cam._br;
			dy = -ry / (rz + _Cam._br) * _Cam._br;
			dz = _Cam._br / (_Cam._br + rz);
	
			return { rx:rx, ry:ry, rz:rz, dx:dx, dy:dy, dz:dz,ux:ux,uy:uy };
		
		}
		private function get_mid_z(a, b, c) {
			var _max = Math.max(a, b, c)
			var _min = Math.min(a, b, c)
			var _mid = a + b + c - _max - _min;
			return _mid
		}
		
    }
	
}


